from copy import deepcopy

import past
from flask import Blueprint, request, jsonify
from controllers.common import *
from controllers.db import *

build_test_proj_api = Blueprint('build_test_proj_api', __name__)
build_test_proj_issue_api = Blueprint('build_test_proj_issue_api', __name__)

@build_test_proj_api.route('/build_test_proj_action', methods=['GET', 'POST'])
def build_test_proj_action():
  params = json.loads(request.data)
  _action = params['action']
  result = politic(_action)(params)
  if result == -1:
    return {'status': 404, 'info': 'action error!'}
  result_data = {'status': 200, 'data': result, 'info': 'request success!'}
  return jsonify(result_data)

@build_test_proj_issue_api.route('/build_test_proj_issue_action', methods=['GET', 'POST'])
def build_test_proj_action():
  params = json.loads(request.data)
  _action = params['action']
  result = issuePolitic(_action)(params)
  if result == -1:
    return {'status': 404, 'info': 'action name error!'}
  return result


def nullMethod(param):
  return -1


def addMethod(param):
  pass

def deleteMethod(param):
  pass

def updateMethod(param):
  pass

def updateListMethod(param):
  pass

def getItemMethod(param):
  pass

def getListMethod(param):
  pass

def politic(_action):
  methods={
    'add_data': addMethod,
    'delete_data': deleteMethod,
    'update_method': updateMethod,
    'update_list_method': updateListMethod,
    'get_item_method': getItemMethod,
    'get_list_method': getListMethod,
  }
  return methods.get(_action, nullMethod)


def createJson(param):
  isPush = 0
  return DownLoad(param.fileName, result_data, isPush)

def pushIssue(param):
  isPush = 1
  return DownLoad(param.fileName, result_data, isPush)

def issuePolitic(_action):
  methods={
    'create_json': createJson,
    'push_issue': pushIssue,
  }
  return methods.get(_action, nullMethod)
